package com.stx.mapper;

import com.stx.bean.Pay;
import com.stx.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*充值管理读取数据库*/
public class PayMapper {
    //获取充值列表
    public static List<Pay> findPaysList() throws SQLException {
        List<Pay> payslist = new ArrayList<>();
        Connection connection = JDBCUtil.getConnection();
        String sql = "select * from pay";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String id = resultSet.getString("id");          //id
            String name = resultSet.getString("name");      //姓名
            String way = resultSet.getString("way");        //充值方式
            String number = resultSet.getString("number");  //充值金额
            String payTime = resultSet.getString("payTime");//充值时间
            Pay pay = new Pay(id, name, way, number, payTime);
            payslist.add(pay);
        }
        return payslist;
    }

/*添加充值*/
    public static Boolean payAdd(Pay pay) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql = "insert into pay(id,name,way,number,payTime) value (?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,pay.getId());
        preparedStatement.setString(2,pay.getName());
        preparedStatement.setString(3,pay.getWay());
        preparedStatement.setString(4,pay.getNumber());
        preparedStatement.setString(5,pay.getPayTime());
        int i = preparedStatement.executeUpdate();
        return true;
    }
    //根据id查找充值
    public static Pay findPayById(String id) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql = "select * from pay where id = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String name = resultSet.getString("name");      //姓名
            String way = resultSet.getString("way");        //充值方式
            String number = resultSet.getString("number");  //充值金额
            String payTime = resultSet.getString("payTime");//充值时间
            Pay pay = new Pay(id,name,way,number,payTime);
            return pay;
        }
        return null;
    }

    //更新充值信息
    public static Boolean payUpdate(Pay pay) throws SQLException{
        Connection connection = JDBCUtil.getConnection();

//        String sql = "update pay set name=?,way=?,number=?,payTime=? where id =?";
        String sql = "update pay set name=?,way=?,number=? where id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,pay.getName());
        preparedStatement.setString(2,pay.getWay());
        preparedStatement.setString(3,pay.getNumber());
        preparedStatement.setString(4, pay.getId());
//        preparedStatement.setString(5, pay.getId());
        int i = preparedStatement.executeUpdate();
        return true;
    }
    //根据id删除充值信息
    public static Boolean delPayById(String id) throws Exception {
        Connection connection = JDBCUtil.getConnection();
        String sql = "delete from pay where id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,id);
        int i = preparedStatement.executeUpdate();
        return true;
    }
}
